package cn.gglol.jvvtool.service.jedis.impl;

import cn.gglol.jvvtool.entity.User;
import cn.gglol.jvvtool.jedis.cache.RefreshType;
import cn.gglol.jvvtool.jedis.cache.annotation.RedisCache;
import cn.gglol.jvvtool.service.jedis.CacheService;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author jvvtao
 * @version 1.0
 * @project Name: jvvtool
 * @file Name: CacheServiceImpl
 * @desc Jedis Cache Service Impl
 * @date 2021/9/1
 * @time 23:14
 * @by IDE: IntelliJ IDEA
 */
@Service
public class CacheServiceImpl implements CacheService {

    @RedisCache(prefix = "jvvtao:", key = "getTime:{0}:{1}", duration = 10)
    @Override
    public String getTime(Integer seconds) {
        return LocalTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME);
    }

    @RedisCache(prefix = "jvvtao:", key = "getTime2:{0}:{1}", duration = 20, refreshType = RefreshType.AUTO_UPDATE)
    @Override
    public String getTime2(Integer seconds) {
        return LocalTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME);
    }

    @RedisCache(prefix = "jvvtao", key = "getUser:{0}", duration = 5, refreshType = RefreshType.AUTO_UPDATE)
    @Override
    public User getUser(Long id) {
        User user = new User();
        user.setId(id);
        user.setUsername("张三");
        user.setTimestamp(LocalTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME));
        return user;
    }

    @RedisCache(prefix = "jvvtao", key = "getUser2:{0}", duration = 5)
    @Override
    public User getUser2(Long id) {
        User user = new User();
        user.setId(id);
        user.setUsername("李四");
        user.setTimestamp(LocalTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME));
        return user;
    }

    @RedisCache(prefix = "jvvtao", key = "getUserList:{0}", duration = 10, collectionGenericsType = User.class)
    @Override
    public List<User> getUserList(Integer count) {
        List<User> list = new ArrayList<>();
        for (Integer i = 0; i < count; i++) {
            list.add(getUser(Long.valueOf(i)));
        }
        return list;
    }

    @RedisCache(prefix = "jvvtao", key = "getUserSet:{0}", duration = 10, collectionGenericsType = User.class)
    @Override
    public Set<User> getUserSet(Integer count) {
        Set<User> set = new HashSet<>();
        for (Integer i = 0; i < count; i++) {
            set.add(getUser(Long.valueOf(i)));
        }
        return set;
    }

    @RedisCache(prefix = "jvvtao", key = "getUserHashSet:{0}", duration = 10, collectionGenericsType = User.class)
    @Override
    public HashSet<User> getUserHashSet(Integer count) {
        HashSet<User> set = new HashSet<>();
        for (Integer i = 0; i < count; i++) {
            set.add(getUser(Long.valueOf(i)));
        }
        return set;
    }
}
